watch: fix --watch-path restart on unrelated file with --env-file#61970
Open
RajeshKumar11 wants to merge 1 commit intonodejs:mainfrom
Open
watch: fix --watch-path restart on unrelated file with --env-file#61970RajeshKumar11 wants to merge 1 commit intonodejs:mainfrom
RajeshKumar11 wants to merge 1 commit intonodejs:mainfrom
Conversation
Codecov Reportβ
All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #61970 +/- ##
==========================================
+ Coverage 88.84% 89.75% +0.91%
==========================================
Files 674 674
Lines 204957 205673 +716
Branches 39309 39429 +120
==========================================
+ Hits 182087 184609 +2522
+ Misses 15088 13313 -1775
+ Partials 7782 7751 -31
π New features to boost your workflow:
|
When both --watch-path=<dir> and --env-file=<path> are provided, touching any file in the directory containing the env file incorrectly triggered a restart, even if the changed file was unrelated to the watched path. Root cause: filterFile() watches dirname(file) recursively on macOS and Windows (platforms with recursive fs.watch support). In 'filter' mode this is harmless because #onChange only fires for files in #filteredFiles. But in 'all' mode (used when --watch-path is set), #onChange fires for any change in any watched path without checking the filter, so watching the env file's parent directory caused restarts on every file change in that directory. Fix: only watch the parent directory in 'filter' mode. In 'all' mode, watch the specific file directly (watchPath(file, false)) so that changes to unrelated files in the same directory are not observed. Fixes: nodejs#61906
869ff22 to
0a288d3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
--watch-path=<dir>together with--env-file=<path>, touching any file in the directory that contains the env file incorrectly triggers a restart β even if the changed file has nothing to do with the watched pathfilterFile()now watches the specific file directly in'all'mode, rather than watching its entire parent directoryRoot cause
FilesWatcherhas two modes:#onChangebehaviour'filter'--watch-path#filteredFiles'all'--watch-pathprovidedfilterFile()inlib/internal/watch_mode/files_watcher.js:131takes a shortcut on macOS and Windows (platforms with recursivefs.watchsupport): instead of setting up one watcher per file, it callswatchPath(dirname(file))to watch the whole parent directory with a single recursive FSWatcher.In
'filter'mode this is harmless β#onChangechecks#filteredFilesbefore emitting'changed', so only the tracked file triggers a restart.In
'all'mode the filter check is skipped. So whenwatch_mode.jscallsfilterFile(resolve('.env'))(line 106) to watch the env file, it actually watches.(the CWD) recursively. Anyfs.watchevent in the CWD β includingtouch unrelatedβ then passes straight through and triggers a restart.Fix
Add
&& this.#mode === 'filter'to the condition that selects the parent-directory strategy:In
'all'mode the env file is now watched directly (watchPath(file, false)), so only changes to that exact file fire the watcher.Tests
Two new cases added to
test/parallel/test-watch-mode-files_watcher.mjs:filterFile()in'all'mode + modify an unrelated file in the same directory βmustNotCallasserts no'changed'event firesfilterFile()in'all'mode + modify the watched file itself β'changed'fires exactly onceRelated
Ref: #61906 (reporter provided a clean minimal repro)
Fixes: #61906